/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui.renderer;

import com.sun.lwuit.Component;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.plaf.UIManager;

/**
 *
 * @author Sun
 */
public class ItemTickerRenderer extends ItemRenderer {

    private List parentList;
    private ItemRenderer selectedRenderer;
    private int currentSelectedIndex = 0;
    private long startTicker;
    private static int DELAY = 1500;

    public ItemTickerRenderer(byte type) {
        super(type);
        selectedRenderer = new ItemRenderer(type);
    }

    public void stopTicker() {
        if (parentList != null) {
            selectedRenderer.getTicker().setShiftText(0);
            Form form = parentList.getComponentForm();
            form.deregisterAnimated(this);
        }
    }

    public void startTickert() {
        if (parentList != null) {
            Form form = parentList.getComponentForm();
            form.registerAnimated(this);
        }
    }

    public boolean animate() {
        if (parentList != null && parentList.getComponentForm() != null) {
            if (selectedRenderer.getTicker().isTickerRunning()) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - startTicker < DELAY) {
                    return false;
                }
                if (selectedRenderer.getTicker().animate()) {
                    parentList.repaint();
                }
            }
        }
        return super.animate();
    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
        if (isSelected) {
            int selectedIndex = list.getSelectedIndex();
            // sometimes the list asks for a dummy selected value for size
            // calculations and this might break the tickering state

            selectedRenderer.getListCellRendererComponent(list, value, index,
                    isSelected);
            Label ticker = selectedRenderer.getTicker();
            if (currentSelectedIndex != selectedIndex) {
                currentSelectedIndex = selectedIndex;
                ticker.setShiftText(0);
                if (ticker.shouldTickerStart()) {
                    startTicker = System.currentTimeMillis();
                }
            }
            if (index == selectedIndex) {
                if (ticker.shouldTickerStart()) {
                    if (!ticker.isTickerRunning()) {
                        parentList = list;
                        list.getComponentForm().registerAnimated(this);
                        ticker.startTicker(UIManager.getInstance().
                                getLookAndFeel().getTickerSpeed(), true);
                    }
                } else {
                    if (ticker.isTickerRunning()) {
                        ticker.stopTicker();
                    }
                }
            }
            return selectedRenderer;
        } else {
            return super.getListCellRendererComponent(list, value, index, isSelected);
        }
    }
}
